/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package xmlparser;

/**
 *
 * @author pbelov
 */
import base.Project;
import java.util.Vector;
import org.xml.sax.helpers.DefaultHandler;
import org.xml.sax.*;

public class projectParser extends DefaultHandler{
    Project project[];
    Vector vp = new Vector();
    Project tproject;

    String thisElement = "";

    public static String PROJECT = "project";
    public static String NAME = "name";
    public static String MANAGER = "manager";

    public projectParser() {
        vp.setSize(0);
    }

    public void fill(Project[] projects) {
        for (int i = 0; i < projects.length; i++) {
            projects[i] = (Project)vp.elementAt(i);
        }
    }

    public int getCount() {
        return vp.size();
    }

    public Project[] getResult(){
      return project;
    }

    @Override
    public void startDocument() throws SAXException {
      System.out.println("Start parse projects XML...");
    }

    @Override
    public void startElement(String namespaceURI, String localName, String qName, Attributes atts) throws SAXException {
        thisElement = qName;

        if (thisElement.equals(PROJECT)) {
            tproject = new Project();
        }
    }

    @Override
    public void endElement(String namespaceURI, String localName, String qName) throws SAXException {
        if (qName.equals(PROJECT)) {
            vp.add(tproject);
            tproject = null;
        }

        thisElement = "";
    }

    @Override
    public void characters(char[] ch, int start, int length) throws SAXException {
        if (thisElement.equals(NAME)) {
            tproject.setName(new String(ch, start, length));
        }

        if (thisElement.equals(MANAGER)) {
            tproject.setManager(new String(ch, start, length));
        }
    }

    @Override
    public void endDocument() {
      System.out.println("Stop parse projects XML...");
    }
}

